/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.artic.jme3gui.client;

/**
 *
 * @author PaulSyntax
 */
import com.artic.jme3gui.client.loader.LoadingState;
import com.jme3.app.Application;
import com.jme3.app.BulletAppState;
import com.jme3.bullet.PhysicsSpace;
import com.jme3.bullet.PhysicsTickListener;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.plugins.ogre.MeshLoader;
import com.jme3.system.AppSettings;
import com.jme3.system.JmeSystem;
import com.jme3.system.Timer;

public class ApplicationClient extends Application implements PhysicsTickListener{

   private GameState gameState = null;
   private MenuState menuState = null;
   private LoadingState loadingState = null;
   private BulletAppState bulletAppState;

   public ApplicationClient() {
   }

    @Override
    public void start(){
        // set some default settings in-case
        // settings dialog is not shown
        if (settings == null)
            setSettings(new AppSettings(true));

        // show settings dialog
        if (!JmeSystem.showSettingsDialog(settings))
           return;

        super.start();
    }

   @Override
    public void initialize() {
      // initialize the standard environment first
      super.initialize();
      assetManager.registerLoader(com.artic.ogre.loader.OgreSceneLoader.class,"scene");
      // assetManager.registerLoader(MeshLoader.class, "meshxml");
      // Create the States
      loadingState = new LoadingState(this);
      menuState = new MenuState(this);
      gameState = new GameState(this);
      bulletAppState=new BulletAppState();
      bulletAppState.startPhysics();

      // Attach MenuState
      getStateManager().attach(loadingState);
      getStateManager().attach(bulletAppState);
      getPhysicsSpace().addTickListener(this);

    }


   @Override
    public void update() {
        super.update();
        float tpf = timer.getTimePerFrame() * speed;
        // update states
        stateManager.update(tpf);
        // render states
        stateManager.render(renderManager);
        renderManager.render(tpf);
    }


   public void loadMenu() {
      getStateManager().detach(gameState);
      getStateManager().attach(menuState);
      getStateManager().detach(loadingState);
   }


   public void loadGame() {
      getStateManager().detach(menuState);
      getStateManager().attach(gameState);
      getStateManager().detach(loadingState);

   }

   public void loadLoding() {
     getStateManager().detach(gameState);
     getStateManager().detach(menuState);
     getStateManager().attach(loadingState);
   }



   public ViewPort getViewPort() {
      return viewPort;
   }

   public ViewPort getGUIViewPort() {
      return guiViewPort;
   }


   public Timer getTimer() {
      return timer;
   }


   public static void main(String... args) {
      new ApplicationClient().start();
   }

    @Override
    public void physicsTick(PhysicsSpace ps, float f) {
        simplePhysicsUpdate(f);
    }

    public PhysicsSpace getPhysicsSpace(){
        return bulletAppState.getPhysicsSpace();
    }
    
    public void simplePhysicsUpdate(float tpf){

    }
}